package data_transfer;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.OutputFormat;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;

public class HDFSToHDFS {

    public static String inputPath = "hdfs://localhost:9000/hdfstohdfs/dat";
    public static String outputPath = "hdfs://localhost:9000/hdfstohdfs/outs";

    public static void main(String[] args)
            throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(inputPath), conf);
        if (!fs.exists(new Path(inputPath))) {
            System.out.println("source file doesn't exists!");
            System.out.println("now exiting ...");
            System.exit(1);
        }
        Job job = Job.getInstance(conf, "HDFSToHDFS");
        FileInputFormat.setInputPaths(job, new Path(inputPath));
        job.setMapperClass(doMapper.class);
        job.setMapOutputValueClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setReducerClass(doReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        if (fs.exists(new Path(outputPath))) {
            System.out.println("output path already exists!");
            System.out.println("now deleting this path ...");
            fs.delete(new Path(outputPath), true);
        }
        FileOutputFormat.setOutputPath(job, new Path(outputPath));
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

    public static class doMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        private IntWritable one = new IntWritable(1);
        private Text word = new Text();

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().trim().split(" ");
            for (String split : splits) {
                word.set(split);
                context.write(word, one);
            }

        }
    }

    public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        private int sum = 0;
        private IntWritable ttl = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            ttl.set(sum);
            context.write(key, ttl);
        }
    }
}
